package com.kxj.gaode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class LocationService extends Service {
	private static final String TAG = "GaodePlugin";
	/**
	 * 定位客户端
	 */
	private AMapLocationClient locationClient;

	/**
	 * 是否是单次定位
	 */
	private static boolean isOnceLocation = false;

	private static Map<Integer, String> locationTypeInfo = new HashMap<Integer, String>();
	static {
		locationTypeInfo.put(0, "定位失败");
		locationTypeInfo
				.put(1, "GPS定位结果-->通过设备GPS定位模块返回的定位结果，精度较高，在10米－100米左右");
		locationTypeInfo.put(2,
				"前次定位结果-->网络定位请求低于1秒、或两次定位之间设备位置变化非常小时返回，设备位移通过传感器感知。");
		locationTypeInfo.put(4, "缓存定位结果-->返回一段时间前设备在同样的位置缓存下来的网络定位结果");
		locationTypeInfo.put(5,
				"Wifi定位结果-->属于网络定位，定位精度相对基站定位会更好，定位精度较高，在5米－200米之间。");
		locationTypeInfo.put(6,
				"基站定位结果-->纯粹依赖移动、联通、电信等移动网络定位，定位精度在500米-5000米之间");
		locationTypeInfo.put(8, "离线定位结果");
	}

	/**
	 * 获取定位结果的类型
	 * 
	 * @param errorCode
	 * @return
	 */
	public static String getLocationTypeInfo(int locationTypeCode) {
		return locationTypeInfo.get(locationTypeCode);
	}

	@Override
	public IBinder onBind(Intent paramIntent) {

		return null;
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// 定位客户端
		locationClient = new AMapLocationClient(this);
		// 定位参数
		AMapLocationClientOption locationOption = new AMapLocationClientOption();
		String jsonStringOptions = intent.getExtras().get("locationOptions")
				.toString();
		try {
			JSONArray jsonArray = new JSONArray(jsonStringOptions);
			if (jsonArray.length() > 0) {
				// cordova插件传递参数只能是JSONArray,js调用该插件时,设置JSONArray里面有只有一个JSONObject
				JSONObject obj = jsonArray.getJSONObject(0);
				if (obj.has("locationMode")) {
					String locationMode = obj.getString("locationMode");
					if ("hight".equals(locationMode)) {
						locationOption
								.setLocationMode(AMapLocationMode.Hight_Accuracy);// 设置定位模式为高精度定位
					} else if ("saving".equals(locationMode)) {
						locationOption
								.setLocationMode(AMapLocationMode.Battery_Saving);// 设置定位模式省电模式
					} else if ("device".equals(locationMode)) {
						locationOption
								.setLocationMode(AMapLocationMode.Device_Sensors);// 设置定位模式为设备模式
					}
				} else {
					locationOption
							.setLocationMode(AMapLocationMode.Hight_Accuracy);
				}
				if (obj.has("isOnceLocation")) {
					isOnceLocation = obj.getBoolean("isOnceLocation");
					locationOption.setOnceLocation(isOnceLocation);
				}

				if (obj.has("isOnceLocationLatest")) {
					isOnceLocation = obj.getBoolean("isOnceLocationLatest");
					locationOption.setOnceLocationLatest(isOnceLocation);
				}

				if (obj.has("interval")) {
					locationOption.setInterval(obj.getLong("interval"));
				}
			}
		} catch (JSONException e) {
			Toast.makeText(this, "定位参数异常", Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		}
		// 设置定位参数
		locationClient.setLocationOption(locationOption);
		// 给客户端设置定位监听
		locationClient.setLocationListener(new MyLocationListener());
		// 开启定位
		locationClient.startLocation();
		Log.i(TAG, "------>启动定位服务");
		return START_STICKY;
	}

	/**
	 * 定位监听,位置改变时，发送广播
	 */
	class MyLocationListener implements AMapLocationListener {
		@Override
		public void onLocationChanged(AMapLocation amapLocation) {
			Intent intent = new Intent("com.kxj.locationChanged");
			intent.putExtra("isOnceLocation", isOnceLocation);
			intent.putExtra("errorCode", amapLocation.getErrorCode());
			//amapLocation.getLocationType()==0;//表示定位失败
			//amapLocation.getErrorCode()==0;//表示定位成功
			if (amapLocation.getLocationType() == 0) {
				// 定位失败
				intent.putExtra("errorInfo", amapLocation.getErrorInfo());
			} else {
				//定位成功
				intent.putExtra("latitude", amapLocation.getLatitude());
				intent.putExtra("longitude", amapLocation.getLongitude());
				intent.putExtra("address", amapLocation.getAddress());
				intent.putExtra("city", amapLocation.getCity());
				intent.putExtra("locationType",
						getLocationTypeInfo(amapLocation.getLocationType()));
			}
			Log.i(TAG, "位置发送改变，发送广播");
			getApplication().sendBroadcast(intent);
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		// 服务销毁，停止定位
		locationClient.stopLocation();
		Log.i(TAG, "------>停止定位服务");
	}

}
